package com.hanhanxiaochong.zhongxiangchuanmei.view.my.act;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.CollectItemBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.page.PageRecorder;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.CollectPresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseLayout;
import com.hanhanxiaochong.zhongxiangchuanmei.view.home.act.HotProjectDetailActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.ICollectView;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.adapter.CollectAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/5/9.
 * Desc   :
 */

public class MyFavoriteProjectLayout extends BaseLayout<CollectPresenter> implements ICollectView {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smartRefresh;

    private CollectAdapter collectAdapter;
    private String token;
    private PageRecorder pageRecorder = new PageRecorder();
    private double lat;
    private double lng;

    public MyFavoriteProjectLayout(Context context) {
        super(context);
    }

    public void setLatLng(double lat, double lng) {
        this.lat = lat;
        this.lng = lng;
        mPresenter.getCollectList(token, "1", lat, lng, pageRecorder.getCurrentPage());
    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_recyclerview;
    }

    @Override
    protected void initPresenter() {
        mPresenter = new CollectPresenter(this);
        token = SharedPreferencesUtil.getsInstances(mContext).getLoginInfo().getToken();
    }

    @Override
    protected void init() {
        List<CollectItemBean> list = new ArrayList<>();
        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        collectAdapter = new CollectAdapter(mContext, R.layout.item_shop, list, "1");
        recyclerview.setAdapter(collectAdapter);
        collectAdapter.setmOnItemClickListener((adapter, position) -> {
            CollectItemBean bean = collectAdapter.getDatas().get(position);
            mContext.startActivity(HotProjectDetailActivity.createIntent(mContext, bean.getId()));
        });
        smartRefresh.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mPresenter.getCollectList(token, "1", lat, lng, pageRecorder.getCurrentPage());
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageRecorder.moveToFirstPage();
                mPresenter.getCollectList(token, "1", lat, lng, pageRecorder.getCurrentPage());
            }
        });
    }

    @Override
    public void setCollectList(List<CollectItemBean> list) {
        smartRefresh.finishRefresh();
        collectAdapter.setDatas(list);
        pageRecorder.moveToNextPage();
    }

    @Override
    public void addCollectList(List<CollectItemBean> list) {
        smartRefresh.finishLoadmore();
        collectAdapter.addDatas(list);
        pageRecorder.moveToNextPage();
    }
}
